package de.dlrg.wms.gui.forms.customForms;

import java.util.HashMap;
import java.util.List;

import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;

import de.dlrg.wms.model.Discipline;
import de.dlrg.wms.model.Swimmer;
import de.dlrg.wms.model.SwimmerTrackRun;
import de.dlrg.wms.model.Track;
import de.dlrg.wms.model.transformed.AgeClassToSwimmer;

public class AgeClassSwimmerDisciplineCombobox extends Composite {
	private List<AgeClassToSwimmer> ageClassToSwimmers;
	private HashMap<Integer, AgeClassToSwimmer> indexAgeClassToSwimmerMapping = new HashMap<Integer, AgeClassToSwimmer>();
	private HashMap<Integer, Swimmer> indexSwimmerMapping = new HashMap<Integer, Swimmer>();
	private HashMap<Integer, Discipline> indexDisciplineMapping = new HashMap<Integer, Discipline>();

	private Combo ageClassToSwimmerCombobox;
	private Combo swimmerCombobox;
	private Combo disciplineCombobox;
	private Track track;
	private SwimmerTrackRun swimmerTrackRun;

	public void setSwimmerTrackRun(SwimmerTrackRun swimmerTrackRun) {
		this.swimmerTrackRun = swimmerTrackRun;
		this.initAll();
	}

	private void initAll() {
		this.initAge();
		this.initSwimmer();
		this.initDisciplines();
	}

	/**
	 * @wbp.parser.constructor
	 */
	public AgeClassSwimmerDisciplineCombobox(Composite arg0, int arg1,
			List<AgeClassToSwimmer> ageClassToSwimmers, Track track) {
		this(arg0, arg1, ageClassToSwimmers, track, new SwimmerTrackRun());
	}

	public AgeClassSwimmerDisciplineCombobox(Composite arg0, int arg1,
			List<AgeClassToSwimmer> ageClassToSwimmers, Track track,
			SwimmerTrackRun swimmerTrackRun) {
		super(arg0, arg1);
		setLayout(new GridLayout(3, false));

		this.track = track;
		this.ageClassToSwimmers = ageClassToSwimmers;
		this.swimmerTrackRun = swimmerTrackRun;

		ageClassToSwimmerCombobox = new Combo(this, SWT.NONE);
		ageClassToSwimmerCombobox.setLayoutData(new GridData(SWT.FILL,
				SWT.CENTER, true, false, 1, 1));

		this.swimmerCombobox = new Combo(this, SWT.NONE);
		swimmerCombobox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		ageClassToSwimmerCombobox.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				initSwimmer();
			}
		});
		swimmerCombobox.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				initDisciplines();
			}
		});
		disciplineCombobox = new Combo(this, SWT.NONE);
		disciplineCombobox.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1));

		this.initAge();
	}

	public SwimmerTrackRun getSwimmerTrackRun() {
		return (this.swimmerTrackRun
				.setDiscipline(getSelectedDiscipline())
				.setSwimmer(getSelectedSwimmer()).setTrack(this.track)
				);
	}

	private void initDisciplines() {
		int i = 0;
		this.indexDisciplineMapping.clear();
		this.disciplineCombobox.removeAll();
		try {
			for (Discipline discipline : this.getSelectedSwimmer()
					.getAgeClass().getDiscipline()) {
				this.disciplineCombobox.add(discipline.getName(), i);
				this.indexDisciplineMapping.put(i, discipline);
				if (discipline.equals(this.swimmerTrackRun.getDiscipline())) {
					this.disciplineCombobox.select(i);
				}
				i++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Discipline getSelectedDiscipline() {
		if (this.indexDisciplineMapping.containsKey(this.disciplineCombobox
				.getSelectionIndex())) {
			return this.indexDisciplineMapping.get(this.disciplineCombobox
					.getSelectionIndex());
		}
		return Discipline.DisciplineNull;
	}

	private Swimmer getSelectedSwimmer() {
		if (this.indexSwimmerMapping.containsKey(this.swimmerCombobox
				.getSelectionIndex())) {
			return this.indexSwimmerMapping.get(this.swimmerCombobox
					.getSelectionIndex());
		}
		return Swimmer.SwimmerNull;
	}

	private void initSwimmer() {
		int i = 0;
		this.indexSwimmerMapping.clear();
		this.swimmerCombobox.removeAll();
		for (Swimmer swimmer : this.getSelectedAgeClassToSwimmer()
				.getSwimmers()) {
			this.swimmerCombobox.add(
					swimmer.getName() + ", " + swimmer.getSurName(), i);
			this.indexSwimmerMapping.put(i, swimmer);
			if (swimmer.equals(this.swimmerTrackRun.getSwimmer())) {
				this.swimmerCombobox.select(i);
			}
			i++;
		}

	}

	private void initAge() {
		int i = 0;
		for (AgeClassToSwimmer ageClassToSwimmer : this.ageClassToSwimmers) {
			this.ageClassToSwimmerCombobox.add(ageClassToSwimmer.getAgeClass()
					.getName(), i);
			this.indexAgeClassToSwimmerMapping.put(i, ageClassToSwimmer);
			if (this.swimmerTrackRun.getSwimmer().getAgeClass()
					.equals(ageClassToSwimmer.getAgeClass())) {
				this.ageClassToSwimmerCombobox.select(i);
			}
			i++;
		}
	}

	private AgeClassToSwimmer getSelectedAgeClassToSwimmer() {
		return this.indexAgeClassToSwimmerMapping
				.get(this.ageClassToSwimmerCombobox.getSelectionIndex());
	}
}
